home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / ATOI.C < prev    next >
Text File  |  1986-05-18  |  1KB  |  55 lines

  1. /* 1.1  01-08-86                          (atoi.c)
  2.  ************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1986        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10.  
  11. /************************************************************************/
  12.  
  13. atoi(s)        /* return integer value of string s up to first
  14.            non-digit                        */
  15. /*----------------------------------------------------------------------*/
  16. STRING s;
  17. {
  18.     int i;
  19.     STRING astoi();
  20.  
  21.     astoi(s, &i);
  22.     return i;
  23. }
  24.  
  25. /************************************************************************/
  26.     STRING
  27. astoi(s, val)    /* store into val the value of the string s up to the
  28.            first non-digit.  return pointer to that char.    */
  29. /*----------------------------------------------------------------------*/
  30. STRING s;
  31. int *val;
  32. {
  33.     int c, v, base;
  34.     BOOL minus;
  35.     METACHAR tobase();
  36.  
  37.     while (isspace(*s))
  38.         s++;
  39.     if ((minus = (*s IS '-')) OR (*s IS '+'))
  40.         s++;
  41.     if (*s IS '0')
  42.         if (tolower(*(++s)) IS 'x')
  43.         {    base = 16;
  44.             s++;
  45.         }
  46.         else
  47.             base = 8;
  48.     else
  49.         base = 10;
  50.     for (v = 0; (c = tobase(*s, base)) >= 0; s++)
  51.         v = v * base + c;
  52.     *val = (minus ? -v : v);
  53.     return s;
  54. }
  55.